using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Race : LoadableJSONObject{
	
	public string Size = "Medium";
	public int Speed = 30;
	public bool Quadruped = false;
	
	public int STRMod = 0;
	public int DEXMod = 0;
	public int CONMod = 0;
	public int INTMod = 0;
	public int WISMod = 0;
	public int CHAMod = 0;
	
	public List<string> racialAbilities = new List<string>{};
	
	private List<Ability> _loadedRacialAbilities;
	public List<Ability> loadedRacialAbilities
	{
		get
		{
			if(_loadedRacialAbilities == null)
			{
				_loadedRacialAbilities = new List<Ability>();
				racialAbilities.ForEach( (a) =>
					{
						if(FeatManager.index.ContainsKey(a))
							_loadedRacialAbilities.Add(FeatManager.Load(a));
						else if(SpecialAbilityManager.index.ContainsKey(a))
							_loadedRacialAbilities.Add(SpecialAbilityManager.Load(a));
					}
				);
			}
			return _loadedRacialAbilities;
		}
	}
	
	public Race()
	{
		description = " ";
	}
	
	public override void Load(string json, string file)
	{
		fileName = file;
		if(json != null)
		{
			Dictionary<string,object> data = MiniJSON.Json.Deserialize(json) as Dictionary<string,object>;
			
			name = data["name"].ToString();
			description = data["description"].ToString();
			Size = data["Size"].ToString();
			Speed = int.Parse(data["Speed"].ToString());
			Quadruped = bool.Parse(data["Quadruped"].ToString());
			
			STRMod = int.Parse(data["STRMod"].ToString());
			DEXMod = int.Parse(data["DEXMod"].ToString());
			CONMod = int.Parse(data["CONMod"].ToString());
			INTMod = int.Parse(data["INTMod"].ToString());
			WISMod = int.Parse(data["WISMod"].ToString());
			CHAMod = int.Parse(data["CHAMod"].ToString());
			
			racialAbilities = new List<string>();
			
			foreach(object ability in data["racialAbilities"] as List<object>)
			{
				racialAbilities.Add(ability.ToString());
			}
		}
	}
	
	public override string ToJSON()
	{
		Dictionary<string,object> data = new Dictionary<string, object>();
		data.Add("name", name);
		data.Add("description", description.ToString());
		data.Add("Size", Size.ToString());
		data.Add("Speed", Speed.ToString());
		data.Add("Quadruped", Quadruped.ToString());
		data.Add("STRMod", STRMod.ToString());
		data.Add("DEXMod", DEXMod.ToString());
		data.Add("CONMod", CONMod.ToString());
		data.Add("INTMod", INTMod.ToString());
		data.Add("WISMod", WISMod.ToString());
		data.Add("CHAMod", CHAMod.ToString());
		
		data.Add("racialAbilities", racialAbilities);
		
		return MiniJSON.Json.Serialize(data);
	}
	
	public Race DeepCopy()
	{
		Race a = new Race();
		a.Load(ToJSON(), fileName);
		return a;
	}
}
